import { get } from "lodash";
import Api from "src/api/Api";
import { OrderState } from "types/type";

class MyOrderApi extends Api {
    constructor() {
        super();
    }

    // 获取订单列表
    getList(
        { orderState, orderCodeMain, pageNum, pageSize }:
            { orderState: OrderState, orderCodeMain: string, pageNum: number, pageSize: number }
    ) {
        return this._get("/my/order/queryOrderList", { orderState, orderCodeMain, pageNum, pageSize });
    }

    // 取消订单
    cancelOrder(orderCodeMain: string) {
        return this._get(`/my/order/cancelOrder/${orderCodeMain}`);
    }

    //确认订单
    ensureOrder(orderCodeMain: string) {
        return this._get(`/my/order/ensureOrder/${orderCodeMain}`);
    }

    // 删除订单
    delete(orderCodeMain: string) {
        return this._get(`/my/order/deleteOrder/${orderCodeMain}`);
    }

    //根据购物车商品和选择快递公司计算运费
    calculateDeliveryPrice(
        shopCartIds: string,
        appAddressId: number,
        distributionModeId: number
    ) {
        return this._get(`/productOrder/calculateDeliveryPrice/${shopCartIds}`, {
            appAddressId,
            distributionModeId
        });
    }

    // 计算订单页面总价格
    calculateOrderPrice(data: {
        couponPrice?: number; // 优惠券金额，未选择优惠券传0.00
        deliveryPrice?: number; // 快递费，包邮则传0.00
        packagePrice?: number; // 打包费用
        productPrice?: number; // 商品价格，从购物车页面关联带过来
    }) {
        return this._get('/productOrder/calculateOrderPrice', data);
    }

    //添加订单
    addOrder(data: {
        shopCartIds: string,
        appAddressId: number,
        couponId?: number,
        distributionModeId: number,
        orderRemark: string,
        packagePriceType: number,
    }) {
        const { shopCartIds, ...other } = data;
        return this._get(`/productOrder/addProductOrder/${shopCartIds}`, other);
    }

    // 发起支付
    pay(orderCodeMain: string) {
        return this._get(`/productOrder/getPayParam/${orderCodeMain}`);
    }

    //根据主订单编号查询支付状态 SUCCESS-支付成功 REFUND-转入退款 NOTPAY-未支付 CLOSED-已关闭
    queryOrderState(orderCodeMain: string = "20250616204841292621") {
        return this._get(`/productOrder/queryOrderState/${orderCodeMain}`);
    }

    //根据快递单号查询物流信息
    async queryDeliveryInfo(deliveryCode: string) {
        const res = await this._get(`/productOrder/queryDeliveryInfo/${deliveryCode}`);
        return JSON.parse(get(res, "data.data", "[]"));
    }

}

const myOrderApi = new MyOrderApi();

export default myOrderApi;
